﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Administrator_SocicalNetworkEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SearchData();

        }
    }
    BizSocialNetwork socicalnetwork = new BizSocialNetwork();
    public int SOCIALNETWORKID
    {
        get
        {
            return int.Parse(Request.QueryString["SOCIALNETWORKID"].ToString());
        }
    }
    public DataTable GetData
    {
        get
        {
            return socicalnetwork.SelectByID(SOCIALNETWORKID);
        }
    }
    public void SearchData()
    {
        DataTable db = GetData;
        txtSocialNetworkName.Text = db.Rows[0]["SOCIALNETWORKNAME"].ToString();
        txtSortDisplay.Text = db.Rows[0]["SORTDISPLAY"].ToString();
        txtLink.Text = db.Rows[0]["LINKURL"].ToString();
        txtDescription.Text = db.Rows[0]["DESCRIPTION"].ToString();
        imgLogo.ImageUrl = "~/LogoSocialNetwork/" + db.Rows[0]["LOGO"].ToString();
        if (db.Rows[0]["STATUS"].ToString().Trim() == "1")
        {
            cbStatus.Checked = true;
        }
    }
    public int GetRandom()
    {
        Random rd = new Random();
        int a = rd.Next(1, 100);
        return a;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {

            int status = 0;
            if (cbStatus.Checked)
            {
                status = 1;
            }
            else
            {
                status = 0;
            }
            DataTable db = GetData;

            string filename = db.Rows[0]["LOGO"].ToString();
            if (fuLogo.HasFile)
            {
                filename = GetRandom() + "" + fuLogo.FileName;
                fuLogo.SaveAs(Server.MapPath("~/LogoSocialNetwork/" + filename));
            }
            string modfied_by = Session["username"].ToString();

            socicalnetwork.tblSocialNetworkUpdate(SOCIALNETWORKID, txtSocialNetworkName.Text, filename, int.Parse(txtSortDisplay.Text), txtLink.Text, txtDescription.Text, modfied_by, status);
            divMessage.InnerHtml = LibraryO2S.O2S_Message.Success("Hệ thống", "Thực hiện thành công");
            Response.Redirect("SocialNetwork.aspx");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = LibraryO2S.O2S_Message.Error("Lỗi", "Cập nhật không thành công phát hiện lỗi " + ex.Message);
        }

    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/Administrator.aspx");

    }
}